package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.io.Serializable;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Update_AddressActivity extends AppCompatActivity {
    Button update_address;
    TextView getperson;
    TextView address;
    TextView phone;
    TextView region;
    Intent intent;
    private TextView mTitleTextView;
    Button delete_button;
    private Button back;
    private int tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_address);
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mTitleTextView.setText("编辑收货地址");
        delete_button = (Button)findViewById(R.id.button_delete);
        delete_button.setVisibility(View.VISIBLE);
        update_address = (Button)findViewById(R.id.address_save);
        getperson = (TextView) findViewById(R.id.address_username);
        address = (TextView)findViewById(R.id.address_name);
        phone = (TextView)findViewById(R.id.address_phone);
        back = (Button)findViewById(R.id.button_backward);
        region = (TextView)findViewById(R.id.address_region);
        intent = this.getIntent();
        getperson.setText(intent.getStringExtra("name"));
        address.setText(intent.getStringExtra("address"));
        phone.setText(intent.getStringExtra("phone"));
        region.setText(intent.getStringExtra("region"));
        tag = getIntent().getIntExtra("tag",0);
        update_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(getperson.length()<=0)
                {
                    Toast.makeText(Update_AddressActivity.this, "收货人不为空", Toast.LENGTH_SHORT).show();
                }else if (address.length()<=0 || region.length()<=0){
                    Toast.makeText(Update_AddressActivity.this, "地址不为空", Toast.LENGTH_SHORT).show();
                }else if(phone.length()<=0 || !isMobileNO(phone.getText().toString())){
                    Toast.makeText(Update_AddressActivity.this, "电话错误", Toast.LENGTH_SHORT).show();
                }else {
                    update_address();
                    if (tag == 1) {
                        Intent intent = new Intent(Update_AddressActivity.this, AddressActivity.class);
                        intent.putExtra("cars", (Serializable) getIntent().getSerializableExtra("cars"));
                        intent.putExtra("tag", 1);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(Update_AddressActivity.this, AddressActivity.class);
                        startActivity(intent);
                    }
                }

            }
        });

        delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete_button();
                if(tag == 1)
                {
                    Intent intent = new Intent(Update_AddressActivity.this,AddressActivity.class);
                    intent.putExtra("cars",(Serializable)getIntent().getSerializableExtra("cars"));
                    intent.putExtra("tag",1);
                    startActivity(intent);
                }
                else {
                    Intent intent = new Intent(Update_AddressActivity.this,AddressActivity.class);
                    startActivity(intent);
                }
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tag == 1)
                {
                    Intent intent = new Intent(Update_AddressActivity.this,AddressActivity.class);
                    intent.putExtra("cars",(Serializable)getIntent().getSerializableExtra("cars"));
                    intent.putExtra("tag",1);
                    startActivity(intent);
                }
                else {
                    Intent intent = new Intent(Update_AddressActivity.this,AddressActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    public void delete_button()
    {
        String url = "http://10.0.2.2:8443/api/deleteAddress";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"addressid\": \"" + Integer.parseInt(intent.getStringExtra("addressid")) + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

    public void  update_address()
    {

        String url = "http://10.0.2.2:8443/api/updateAddress";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"addressid\": \"" + Integer.parseInt(intent.getStringExtra("addressid")) + "\",\n" +
                "  \"name\": \"" + getperson.getText().toString() + "\",\n" +
                "  \"address\": \"" + address.getText().toString() + "\",\n" +
                "  \"region\": \"" + region.getText().toString() + "\",\n" +
                "  \"phone\": \"" + phone.getText().toString() + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }
    public static boolean isMobileNO(String mobileNums) {
        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        return mobileNums.matches(telRegex);
    }
}